﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Runtime.Serialization.Formatters;
using Yugi.Client.Card;

namespace YugiCard
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {

            InitializeComponent();
            
            Canvas.SetLeft(Card, 0);
            Canvas.SetTop(Card, 0);
            Field.Children.Add(Card);
            defaultvalue();
            comboBox3.SelectedIndex = 0;
            //Card.Atk.Opacity = 0;
           // Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_fusion.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
           /* Card.Detail.Height = 100;
            Canvas.SetTop(Card.Detail, 444);
            Card.textBlock12.Text = Card.textBlock4.Text = Card.textBlock5.Text = "";*/
        }
        Microsoft.Win32.OpenFileDialog open;
        CardControl Card = new CardControl();
         

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Canvas.SetZIndex(image3, 0);
            open = new Microsoft.Win32.OpenFileDialog();
            open.FileOk += new System.ComponentModel.CancelEventHandler(open_FileOk);
            open.ShowDialog();
        }

        void open_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Card.image1.Source = new BitmapImage(new Uri(open.FileName));
        }

        private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
        {
            Card.Title.Text = textBox4.Text; 
        }

        private void textBox3_TextChanged(object sender, TextChangedEventArgs e)
        {
            Card.Detail.Text = textBox3.Text;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Card.AttCan.Children.Clear();
            System.Windows.Controls.Image img = new Image();
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                   
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.light.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                    break;
                case 1:
                    
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.dark.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                    break;
                case 2:
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.fire.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                    break;
                case 3:
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.water.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                    break;
                case 4:
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.wind.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                    break;
                case 5:
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.earth.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                    break;
                case 6:
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.divine.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                    break;
            }
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Card.StarCan.Children.Clear();
            Card.Star = comboBox2.SelectedIndex + 1;
            int x = 280;
            int y = 4;
            for (int i = 0; i < comboBox2.SelectedIndex + 1; i++)
            {
                System.Windows.Controls.Image img = new Image();
                img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.star2.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                Card.StarCan.Children.Add(img);
                Canvas.SetTop(img, y);
                Canvas.SetLeft(img, x);
                x -= 25;

            }
        }

        private void textBox5_TextChanged(object sender, TextChangedEventArgs e)
        {
            Card.Atk.Text = textBox5.Text;
        }

        private void textBox6_TextChanged(object sender, TextChangedEventArgs e)
        {
            Card.Def.Text = textBox6.Text;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog() ;
            save.Filter = "bin|*.bin";
            save.FileOk += new System.ComponentModel.CancelEventHandler(save_FileOk);
            save.FileName = "Card_ID.bin";
            save.ShowDialog();
        }

        void getimage(YBaseCard card)
        {
            int width = 284;
            int height = 287;
     //       Field.Children.Remove(Card);
            this.image3.Source = card.displayImage;
        //   Card.template.Source = card.displayImage;
            RenderTargetBitmap render = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(this.image3);
                dc.DrawRectangle(vb, null, new Rect(59, 124, width, height));
            }
            render.Render(dv);
            this.image2.Source = render;
          //  Card.image1.Source = render;
        }

        void getimage1(YBaseCard card)
        {
          //  BitmapImage img1=new BitmapImage();
            int width = 400;
            int height = 580;
          //  Card.template.Source = card.displayImage;
            RenderTargetBitmap render = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(this.Card);
                dc.DrawRectangle(vb, null, new Rect(0, 0, width, height));
            }
            render.Render(dv);
            this.image3.Source = render;
        }

        void save_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //int width = 284; 
            //int height = 287 ;
            int width = 400;
            int height = 580;
            Microsoft.Win32.SaveFileDialog save = sender as Microsoft.Win32.SaveFileDialog;
            RenderTargetBitmap render = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);


            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(this.Card);
                dc.DrawRectangle(vb, null, new Rect(0, 0, width, height));
            }
            render.Render(dv);
            PngBitmapEncoder png = new PngBitmapEncoder();
            JpegBitmapEncoder jpg = new JpegBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(render));
            jpg.Frames.Add(BitmapFrame.Create(render));
            // Save File Binary
            SaveBinary(png, save.FileName);
            //save File XML
            //Save_Xml(png, save.FileName);
            //SaveImage(png, save.FileName);

        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            save.Filter = "png|*.png";
            save.FileOk += new System.ComponentModel.CancelEventHandler(savecard_FileOk);
            save.FileName = "Card_ID.png";
            save.ShowDialog();

        }
        void savecard_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            int width = 400;
            int height = 580;
            Microsoft.Win32.SaveFileDialog save = sender as Microsoft.Win32.SaveFileDialog;
            RenderTargetBitmap render = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);


            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(this.Card);
                dc.DrawRectangle(vb, null, new Rect(0, 0, width, height));
            }
            render.Render(dv);
            PngBitmapEncoder png = new PngBitmapEncoder();
            JpegBitmapEncoder jpg = new JpegBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(render));
            jpg.Frames.Add(BitmapFrame.Create(render));
            // Save File Binary
            // SaveBinary(png, save.FileName);
            //save File XML
            //Save_Xml(png, save.FileName);
            SaveImage(png, save.FileName);
        }
        private void SaveImage(PngBitmapEncoder png, string name)
        {
            using (Stream stm = File.Create(name))
            {
                png.Save(stm);

            }

        }
        private void SaveBinary(PngBitmapEncoder png, string name)
        {
            long img_length;
            MemoryStream stm_mem = new MemoryStream();

            png.Save(stm_mem);
            img_length = stm_mem.Position;
            YBaseCard card;
            // Generate Data
            if (comboBox3.SelectedIndex == 2 || comboBox3.SelectedIndex == 3)
            {
                if (comboBox3.SelectedIndex == 2)
                {
                    card = new YSpellCard();
                    Generate_Card_Model2((YSpellCard)card);
                }
                else
                {
                    card = new YTrapCard();
                    Generate_Card_Model3((YTrapCard)card);
                }
            }

            else
            {
                card = new YMonsterCard();
                Generate_Card_Model1((YMonsterCard)card);
            }
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serial = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            // Save File
            byte[] img_length_data = BitConverter.GetBytes(img_length);

            Stream stm = File.Create(name);

            // Write length Image
            stm.Write(img_length_data, 0, img_length_data.Length);
            // Write  Image

            copyStream(ref stm, ref stm_mem);
            // Write  data
            serial.Serialize(stm, card);

            stm_mem.Dispose();
            stm.Dispose();
        }
        private void copyStream(ref Stream str, ref  MemoryStream mem)
        {
            byte[] buffer = mem.GetBuffer();
            str.Write(buffer, 0, (int)mem.Length);

        }
        private void Generate_Card_Model1(YMonsterCard card)
        {
           // getimage1(card);
           // card.displayImage = this.image3.Source;
            card.Template = (template)comboBox3.SelectedIndex;
            card.Name = Card.Title.Text;
            card.Description = Card.Detail.Text;
            card.Script = this.Script.Text;
            card.ID = Int32.Parse(Card.ID.Text);
            card.Rights = Card.Rights.Text;
            card.Card_TypeID = 0;
        //    card.displayImage = this.image3.Source;

            card.Level = Card.Star;
            card.CardType = YType.Monster;
            card.Attribute = ( MtrAttribute) comboBox1.SelectedIndex;
            // Damage. Def
            card.Damage = Int32.Parse(Card.Atk.Text);
            card.Defend = Int32.Parse(Card.Def.Text);
            //Script
           
            if (IsEffect.IsChecked == true)
                card.IsEffect = true;
            else
                card.IsEffect = false;
            card.Type = (MtrType)comboBox4.SelectedIndex;
            card.ID = Int32.Parse(Card.ID.Text);
            card.Rights = Card.Rights.Text;
            card.Card_TypeID = 0;
            card.Speed = Card.speed;
          //  card.displayImage = Card.image1.Source;
        }
        private void Generate_Card_Model2(YSpellCard card)
        {
        //    getimage1(card);
        //    card.displayImage = this.image3.Source;
            card.Template = (template)comboBox3.SelectedIndex;
            card.Type = (SpellType)typespell.SelectedIndex;
            card.Name = Card.Title.Text;
            card.Description = Card.Detail.Text;
            card.Script = this.Script.Text;
            card.ID = Int32.Parse(Card.ID.Text);
            card.Rights = Card.Rights.Text;
            card.Card_TypeID = 0;
          //  card.displayImage = Card.image1.Source;
            card.CardType = YType.Spell;
            card.Speed = Card.speed;
        }
        private void Generate_Card_Model3(YTrapCard card)
        {
         //   getimage1(card);
        //    card.displayImage = this.image3.Source;
            card.Template = (template)comboBox3.SelectedIndex;
            card.Type = (TrapType)typetrap.SelectedIndex;
            card.Name = Card.Title.Text;
            card.Description = Card.Detail.Text;
            card.Script = this.Script.Text;
            card.ID = Int32.Parse(Card.ID.Text);
            card.Rights = Card.Rights.Text;
            card.Card_TypeID = 0;
          //  card.displayImage = Card.image1.Source;
            card.CardType = YType.Trap;
            card.Speed = Card.speed;
        }
        private void Load_Card_Model2(YSpellCard card)
        {
            Canvas.SetZIndex(image3, 100);
            this.image3.Source = card.displayImage;
            RectangleGeometry clip = new RectangleGeometry();
            clip.Rect = new Rect(58, 124, 289, 289);
            image3.Clip = clip;
        
            comboBox3.SelectedIndex = (int)card.Template;
            Card.Title.Text = card.Name;
            textBox4.Text = card.Name;
            textBox3.Text = card.Description;
            textBox1.Text = card.ID.ToString();
            textBox2.Text = card.Rights;
            Card.Detail.Text = card.Description;
         //   Card.image1.Visibility = Visibility.Visible;
         //   Card.image1.Source = this.image2.Source;
            //Script
            Card.Script = card.Script;
            this.Script.Text = card.Script;
            Card.ID.Text = card.ID.ToString();
            Card.Rights.Text = card.Rights;
            typespell.SelectedIndex = (int)card.Type;
          //  Field.Children.Add(Card);
            Card.speed = card.Speed;
        }
        private void Load_Card_Model3(YTrapCard card)
        {
         
            comboBox3.SelectedIndex = (int)card.Template;
            Card.Title.Text = card.Name;
            Card.Detail.Text = card.Description;
        
            Canvas.SetZIndex(image3, 100);
            this.image3.Source = card.displayImage;
            RectangleGeometry clip = new RectangleGeometry();
            clip.Rect = new Rect(58, 124, 289, 289);
            image3.Clip = clip;
          
            textBox4.Text = card.Name;
            textBox3.Text = card.Description;
            textBox1.Text = card.ID.ToString();
            textBox2.Text = card.Rights;
            Card.Script = card.Script;
            this.Script.Text = card.Script;
            Card.ID.Text = card.ID.ToString();
            Card.Rights.Text = card.Rights;
            typetrap.SelectedIndex = (int)card.Type;
            Card.speed = card.Speed;
        }
        private void Load_Card_Model(YMonsterCard card)
        {
            Canvas.SetZIndex(image3, 100);
            this.image3.Source = card.displayImage;
            RectangleGeometry clip = new RectangleGeometry();
            clip.Rect = new Rect(58, 124, 289, 289);
            image3.Clip = clip;

            comboBox3.SelectedIndex = (int)card.Template;
            comboBox4.SelectedIndex = (int)card.Type;
            if (card.IsEffect == true)
                IsEffect.IsChecked = true;
            else
                IsEffect.IsChecked = false;
            Card.Title.Text = card.Name;
            Card.Detail.Text = card.Description;
            Card.Star = card.Level;
            this.comboBox2.SelectedIndex = card.Level -1;
            Card.Atk.Text = card.Damage.ToString();
            Card.Def.Text = card.Defend.ToString();
            comboBox1.SelectedIndex = (int)card.Attribute;
            textBox4.Text = card.Name;
            textBox3.Text = card.Description;
            textBox5.Text = card.Damage.ToString();
            textBox6.Text = card.Defend.ToString();
            textBox1.Text = card.ID.ToString();
            textBox2.Text = card.Rights;
           
          //  Card.image1.Visibility = Visibility.Visible;
          //  Card.image1.Source = card.displayImage;
            //Script
            Card.Script = card.Script;
            this.Script.Text = card.Script;
            Card.ID.Text = card.ID.ToString();
            Card.Rights.Text = card.Rights;
            Card.speed = card.Speed;

        }
        private void Load_Click_1(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "bin|*.bin";
            open.FileOk += new System.ComponentModel.CancelEventHandler(open_FileOk1);
            open.ShowDialog();
        }

        void open_FileOk1(object sender, System.ComponentModel.CancelEventArgs e)
        {

            OpenFileDialog open = sender as OpenFileDialog;
            Yugi.Client.Card.FileDecoder.YFileDecoder decoder = new Yugi.Client.Card.FileDecoder.YFileDecoder();

            YBaseCard card = decoder.BinaryFileDecoderCard(open.FileName);
            if (card.CardType == YType.Monster)
            {
                Load_Card_Model((YMonsterCard)card);
                Card.DataContext = card;
            }
            if (card.CardType == YType.Spell)
            {
                Load_Card_Model2((YSpellCard)card);
                Card.DataContext = card;
            }
            if (card.CardType == YType.Trap)
            {
                Load_Card_Model3((YTrapCard)card);
                Card.DataContext = card;
            }
                  
             
            
        }

        private void comboBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            System.Windows.Controls.Image img = new Image();
            switch (comboBox3.SelectedIndex)
            {
                case 0:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_normal.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 1:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_effect.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 2:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_spell.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    
                    break;
                case 3:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_trap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 4:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_fusion.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 5:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_ritual.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 6:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_synchro.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 7:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_dsynchro.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 8:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_dragons.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 9:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_token.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 10:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_slifer.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 11:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_obelisk.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;
                case 12:
                    Card.template.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.card_ra.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); ChangeTemplate();
                    break;

            }
            

        }

        private void comboBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //IsEffect.IsEnabled = true;
            ChangeType();  
        }

        private void IsEffect_Checked(object sender, RoutedEventArgs e)
        {
            effectspeed.Opacity = 100;
            effectspeed.IsEnabled = true;
            ChangeType();
        }
        private void ChangeType()
        {
            if ((comboBox3.SelectedIndex == 4) || (comboBox3.SelectedIndex == 5) || (comboBox3.SelectedIndex == 6))
            {
                if (IsEffect.IsChecked == false)
                    Card.type.Text = "[" + ((MtrType)comboBox4.SelectedIndex).ToString() +" / "+ ((template)comboBox3.SelectedIndex).ToString()+ "]";
                else
                    Card.type.Text = "[" + ((MtrType)comboBox4.SelectedIndex).ToString() + " / " + ((template)comboBox3.SelectedIndex).ToString() + " / Effect]";
            }
            else
            {
                if (IsEffect.IsChecked == false)
                    Card.type.Text = "[" + ((MtrType)comboBox4.SelectedIndex).ToString() +  "]";
                else
                    Card.type.Text = "[" + ((MtrType)comboBox4.SelectedIndex).ToString() + " / Effect]";
            }
        }

        private void typespell_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Card.type_spell_trap.Children.Clear();
            System.Windows.Controls.Image img = new Image();
            switch (typespell.SelectedIndex)
            {
                case 0:
                    Card.spell_trap.Text = "   [ Spell Card ]";
                    Card.speed = 1;
                    break;
                case 1:
                    Card.spell_trap.Text = "   [    Spell Card]";
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.equipment.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.type_spell_trap.Children.Add(img);
                    Card.speed = 1;
                    break;
                case 2:
                    Card.spell_trap.Text = "   [    Spell Card]";
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.field.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.type_spell_trap.Children.Add(img);
                    Card.speed = 1;
                    break;
                case 3:
                    Card.spell_trap.Text = "   [    Spell Card]";
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.quickplay.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.type_spell_trap.Children.Add(img);
                    Card.speed = 2;
                    break;
                case 4:
                    Card.spell_trap.Text = "   [    Spell Card]";
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.ritual.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.type_spell_trap.Children.Add(img);
                    Card.speed = 1;
                    break;
                case 5:
                    Card.spell_trap.Text = "   [    Spell Card]";
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.continuous.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.type_spell_trap.Children.Add(img);
                    Card.speed = 1;
                    break;
            }
        }

        private void typetrap_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Card.type_spell_trap.Children.Clear();
            System.Windows.Controls.Image img = new Image();
            switch (typetrap.SelectedIndex)
            {
                case 0:
                    Card.spell_trap.Text = "   [ Trap Card ]";
                    Card.speed = 2;
                    break;
                case 1:
                    Card.spell_trap.Text = "   [    Trap Card]";
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.continuous.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.type_spell_trap.Children.Add(img);
                    Card.speed = 2;
                    break;
                case 2:
                    Card.spell_trap.Text = "   [    Trap Card]";
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.counter.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.type_spell_trap.Children.Add(img);
                    Card.speed = 3;
                    break;
            }
        }

        private void effectspeed_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (effectspeed.SelectedIndex)
            {
                case 0:
                    Card.speed = 1;
                    break;
                case 1:
                    Card.speed = 2;
                    break;
            }
        }

        private void ChangeTemplate()
        {
            effectspeed.Opacity = 0;
            effectspeed.IsEnabled = false;
            System.Windows.Controls.Image img = new Image();
            if (comboBox3.SelectedIndex == 2 || comboBox3.SelectedIndex == 3)
            {
                Card.Detail.Height = 100;
                Canvas.SetTop(Card.Detail, 444);
                Card.textBlock12.Text = Card.textBlock4.Text = Card.textBlock5.Text = "";
                FrameworkElement[] ctr = { comboBox1, textBlock3 ,comboBox2 ,textBlock8,comboBox4 ,textBlock5,
                                         textBlock9 , textBlock10,textBox5,textBox6,IsEffect};
                controlunable(ctr);
                FrameworkElement[] ctr1= {typetrap, typespell ,textBlock6};
                controlable(ctr1);
                if (comboBox3.SelectedIndex == 2)
                {
                    typetrap.Opacity = 0; typetrap.IsEnabled = false;
                    typespell.Opacity = 100; typespell.IsEnabled = true;
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.spell.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                }
                if (comboBox3.SelectedIndex == 3)
                {
                    typespell.Opacity = 0; typespell.IsEnabled = false;
                    typetrap.Opacity = 100; typetrap.IsEnabled = true;
                    img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(Resource1.trap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    Card.AttCan.Children.Add(img);
                }
            }
            if (comboBox3.SelectedIndex!=2 && comboBox3.SelectedIndex!=3)
            {
                if (comboBox3.SelectedIndex == 1)
                    IsEffect.IsChecked = true;
                Card.Detail.Height = 60;
                Canvas.SetTop(Card.Detail, 462);
                Card.textBlock12.Text = "_____________________________________________________";
                Card.textBlock4.Text = "ATK/"; Card.textBlock5.Text = "DEF/";
                FrameworkElement[] ctr = { comboBox1, textBlock3 ,comboBox2 ,textBlock8,comboBox4 ,textBlock5,
                                         textBlock9 , textBlock10,textBox5,textBox6,IsEffect};
                controlable(ctr);
                Card.AttCan.Children.Clear();
                FrameworkElement [] ctr1={typetrap , typespell , textBlock6};
                controlunable(ctr1);
                if (comboBox3.SelectedIndex == 0)
                    IsEffect.IsEnabled = false;
            }
        }
        private void defaultvalue()
        {
            comboBox1.SelectedIndex = comboBox2.SelectedIndex  = comboBox4.SelectedIndex = typespell.SelectedIndex=typetrap.SelectedIndex=-1;
            textBox3.Text = textBox4.Text = textBox5.Text = textBox6.Text = Card.spell_trap.Text="";
            Card.StarCan.Children.Clear();
            Card.type_spell_trap.Children.Clear();
            IsEffect.IsChecked = false;
            Card.type.Text = "";

        }
        private void controlunable(FrameworkElement [] x)
        {
            defaultvalue();
            for (int i = 0; i < x.Length; i++)
            {
                x[i].Opacity = 0;
                x[i].IsEnabled = false;
            }
           
        }
        private void controlable(FrameworkElement[] x)
        {
            defaultvalue();
            for (int i = 0; i < x.Length; i++)
            {
                x[i].Opacity = 100;
                x[i].IsEnabled = true;
             
            }
           
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            Card.ID.Text = textBox1.Text;
        }

        private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
        {
            Card.Rights.Text = textBox2.Text;
        }
        private void TextBox_GotFocus_1(object sender, RoutedEventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "python|*.py";
            open.FileOk += new System.ComponentModel.CancelEventHandler(open_file_script);
            open.ShowDialog();
        }

        void open_file_script(object sender, System.ComponentModel.CancelEventArgs e)
        {
            OpenFileDialog open = sender as OpenFileDialog;
            StreamReader sr = new StreamReader(open.OpenFile());
            string s = sr.ReadToEnd();

            sr.Close();
        }

        private void Script_TextChanged(object sender, TextChangedEventArgs e)
        {
            Card.script = this.Script.Text;
        }

       
       
        
      
        
    }
}
       